1. Field of the Invention
The present invention relates to the design of registers within computer systems. More specifically, the present invention relates to a method and an apparatus for atomically changing selected bits within a register without changing other bits in the register.
2. Related Art
Modern computing systems provide status registers that allow the computer system to update and maintain the status of various subunits and components within the computing system. One or more bits within a status registers typically contain state information for an associated component in the computer system. Moreover, a single register can include bits for multiple unrelated components within the computer system. During operation, a processor within the computing system can read a status register and examine certain bits to determine the state of a specific component.
A processor can also change bits within a status register by first reading the status register, changing the desired bits, and writing the result back to the register. However, changing bits in this way can be a problem in systems that support multiple threads of execution. After a first thread has read the register, a second thread can possibly write to the register before the first thread is able to write the changed bits to the register. In this case, the subsequent write by the first thread overwrites the changes made by the second thread.
In order to ensure against this potential problem, the register can be protected by a mutual exclusion variable (mutex) or semaphore. To modify a register that is protected by a mutex variable, a process must first gain control of the mutex variable. Next, the process reads the register, modifies the desired bits and writes the modified dataword back to the register. The process then releases the mutex to allow other processes to access the register. By protecting the register in this way, one thread is forced to complete its update before allowing another thread to start an update. This effectively makes the read-modify-write process atomic or uninterruptible.
While using a mutex ensures correctness for the register operations, using the mutex requires additional time to set and clear the mutex. Moreover, the mutex can cause contention problems because other processes may have to wait for the mutex to be released.
Hence, what is needed is a method and an apparatus for changing selected bits within a register without the performance problems described above.